package cn.com.jinhao.provider.business.service.impl;

import cn.com.jinhao.hotline.commons.base.constant.SystemConstant;
import cn.com.jinhao.hotline.commons.exception.BusinessErrorException;
import cn.com.jinhao.provider.business.entity.WorkTerminalCache;
import cn.com.jinhao.provider.business.mapper.WorkTerminalCacheMapper;
import cn.com.jinhao.provider.business.service.WorkTerminalCacheService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * hotline-spring-cloud
 * <p>Description: </p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2020/6/8
 * @since 2020/6/8
 */
@Service
public class WorkTerminalCacheServiceImpl implements WorkTerminalCacheService {
    @Resource
    WorkTerminalCacheMapper workTerminalCacheMapper;

    @Override
    public void add(WorkTerminalCache workTerminalCache) throws Exception {
        // 检查callId是否存在
        if (Optional.ofNullable(this.selectByCallId(workTerminalCache.getCallId())).isPresent()) {
            throw new BusinessErrorException(SystemConstant.ErrorType.REPEAT_ERROR.getName() + "错误编码为: " + SystemConstant.ErrorType.REPEAT_ERROR.getCode() + "错误原因：callId重复");
        }
        int result = workTerminalCacheMapper.insertSelective(workTerminalCache);
        if (result == 0) {
            throw new BusinessErrorException(SystemConstant.ErrorType.ADD_ERROR.getName() + ",错误编码为: " + SystemConstant.ErrorType.ADD_ERROR.getCode());
        }
    }

    @Override
    public void update(WorkTerminalCache workTerminalCache) throws Exception {
        Example example = new Example(WorkTerminalCache.class);
        example.createCriteria().andEqualTo("callId", workTerminalCache.getCallId());
        int result = workTerminalCacheMapper.updateByExample(workTerminalCache, example);
        if (result == 0) {
            throw new BusinessErrorException(SystemConstant.ErrorType.UPDATE_ERROR.getName() + ",错误编码为: " + SystemConstant.ErrorType.UPDATE_ERROR.getCode());
        }
    }

    @Override
    public void delete(String callId) throws Exception {
        Example example = new Example(WorkTerminalCache.class);
        example.createCriteria().andEqualTo("callId", callId);
        int result = workTerminalCacheMapper.deleteByExample(example);
        if (result == 0) {
            throw new BusinessErrorException(SystemConstant.ErrorType.DELETE_ERROR.getName() + ",错误编码为: " + SystemConstant.ErrorType.DELETE_ERROR.getCode());
        }
    }

    @Override
    public List<String> selectByOperCodeAndDeptCode(String operCode, String deptCode) throws Exception {
        return workTerminalCacheMapper.selectCallIdByOperCodeAndDeptCode(operCode,deptCode);
    }

    @Override
    public WorkTerminalCache selectByCallId(String callId) {
        Example example = new Example(WorkTerminalCache.class);
        example.createCriteria().andEqualTo("callId", callId);
        return workTerminalCacheMapper.selectOneByExample(example);
    }
}
